package com.yungu.swift.lbs.trace.baidu.util;

import com.yungu.swift.lbs.trace.baidu.LBSTraceClient;
import com.yungu.swift.lbs.trace.baidu.model.OnUploadListener;
import com.yungu.swift.lbs.trace.baidu.model.TrackPoint;
import com.yungu.swift.lbs.trace.baidu.track.*;
import com.yungu.swift.utils.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

import static com.yungu.swift.lbs.config.NacosListener.THIRDPARTY_CONFIG;

/**
 * 百度鹰眼操作类
 *
 * @author cuixiuyin
 */
public class BaiduTraceUtils {

    public static LBSTraceClient client = LBSTraceClient.getInstance();
    private static long serviceId = 113114;
    private static String ak = "X1Fu6kUc1Ltuoc03KgepnUhytePSU3WQ";
    private static AtomicLong mSequenceGenerator = new AtomicLong();
    private static AtomicInteger successCounter = new AtomicInteger();
    private static AtomicInteger failedCounter = new AtomicInteger();

    static {
        client.init();
        client.start();
        client.registerUploadListener(new OnUploadListener() {
            @Override
            public void onSuccess(long responseId) {
                System.out.println("上传成功 : " + responseId + ", successCounter : " + successCounter.incrementAndGet());
            }

            @Override
            public void onFailed(UploadResponse response) {
                System.err.println("上传失败 : " + response.getResponseID() + ", failedCounter : "
                        + failedCounter.incrementAndGet() + ", " + response);
            }
        });
    }

    /**
     * 异步批量上传轨迹点
     *
     * @param entityName
     * @param trackPoints
     */
    public static void uploadPoints(String entityName, List<TrackPoint> trackPoints) {
        Map<String, List<TrackPoint>> trackPointMap = new HashMap<>();
        String serviceIdValue = THIRDPARTY_CONFIG.getBaiduServiceId();
        serviceId = Long.parseLong(serviceIdValue);
        ak = THIRDPARTY_CONFIG.getBaiduAK();
        trackPointMap.put(entityName, trackPoints);
        AddPointsRequest request = new AddPointsRequest(mSequenceGenerator.incrementAndGet(), ak, serviceId, trackPointMap);
        // 批量添加轨迹点
        client.addPoints(request);
    }

    /**
     * 异步单个上传轨迹点
     *
     * @param entityName
     * @param trackPoint
     */
    public static void uploadPoint(String entityName, TrackPoint trackPoint) {
        String serviceIdValue = THIRDPARTY_CONFIG.getBaiduServiceId();
        serviceId = Long.parseLong(serviceIdValue);
        ak = THIRDPARTY_CONFIG.getBaiduAK();
        AddPointRequest request = new AddPointRequest(mSequenceGenerator.incrementAndGet(), ak, serviceId, entityName, trackPoint);
        client.addPoint(request);
    }

    /**
     * 同步批量上传轨迹点
     *
     * @param entityName
     * @param trackPoints
     */
    public static void uploadPointsSync(String entityName, List<TrackPoint> trackPoints) {
        Map<String, List<TrackPoint>> trackPointMap = new HashMap<>();
        String serviceIdValue = THIRDPARTY_CONFIG.getBaiduServiceId();
        serviceId = Long.parseLong(serviceIdValue);
        ak = THIRDPARTY_CONFIG.getBaiduAK();
        trackPointMap.put(entityName, trackPoints);
        AddPointsRequest request = new AddPointsRequest(mSequenceGenerator.incrementAndGet(), ak, serviceId, trackPointMap);
        // 批量添加轨迹点
        client.addPointsSync(request);
    }

    /**
     * 同步单个上传轨迹点
     *
     * @param entityName
     * @param trackPoint
     */
    public static void uploadPointSync(String entityName, TrackPoint trackPoint) {
        String serviceIdValue = THIRDPARTY_CONFIG.getBaiduServiceId();
        serviceId = Long.parseLong(serviceIdValue);
        ak = THIRDPARTY_CONFIG.getBaiduAK();
        AddPointRequest request = new AddPointRequest(mSequenceGenerator.incrementAndGet(), ak, serviceId, entityName, trackPoint);
        client.addPointSync(request);
    }

    /**
     * 获取距离
     *
     * @param entityName
     * @param startTime
     * @param endTime
     * @param optionalParam
     * @return
     */
    public static DistanceResponse getDistance(String entityName, long startTime, long endTime, Map<String, String> optionalParam) {
        String serviceIdValue = THIRDPARTY_CONFIG.getBaiduServiceId();
        serviceId = Long.parseLong(serviceIdValue);
        ak = THIRDPARTY_CONFIG.getBaiduAK();
        //如果map为空的话，先默认一些查询策略
        String isProcessed = "1";
        String processOption = "need_denoise=1,need_mapmatch=1,radius_threshold=30,transport_mode=driving";
        String supplementMode = "driving";
        if (optionalParam != null && !optionalParam.isEmpty()) {
            if (StringUtils.isNotEmpty(optionalParam.get("isProcessed"))) {
                isProcessed = optionalParam.get("isProcessed");
            }
            if (StringUtils.isNotEmpty(optionalParam.get("processOption"))) {
                processOption = optionalParam.get("processOption");
            }
            if (StringUtils.isNotEmpty(optionalParam.get("supplementMode"))) {
                supplementMode = optionalParam.get("supplementMode");
            }
        }
        GetDistanceRequest getDistanceRequest = new GetDistanceRequest(mSequenceGenerator.incrementAndGet(), ak, serviceId,
                entityName, startTime, endTime, isProcessed, processOption, supplementMode);
        DistanceResponse distanceResponse = client.getDistinct(getDistanceRequest);
        return distanceResponse;
    }

    /**
     * 获取默认配置参数的距离
     *
     * @param entityName
     * @param startTime
     * @param endTime
     * @return
     */
    public static double getDistanceDefault(String entityName, long startTime, long endTime) {
        DistanceResponse distanceResponse = getDistance(entityName, startTime, endTime, null);
        if (distanceResponse != null) {
            return distanceResponse.getDistance();
        }
        return 0d;
    }

    /**
     * 轨迹查询与纠偏
     *
     * @param entityName
     * @param startTime
     * @param endTime
     * @param optionalParam
     * @return
     */
    public static TrackResponse getTrack(String entityName, long startTime, long endTime, Map<String, String> optionalParam) {
        //如果map为空的话，先默认一些查询策略
        String serviceIdValue = THIRDPARTY_CONFIG.getBaiduServiceId();
        serviceId = Long.parseLong(serviceIdValue);
        ak = THIRDPARTY_CONFIG.getBaiduAK();
        String isProcessed = "1";
        String processOption = "need_denoise=1,need_mapmatch=1,radius_threshold=20,transport_mode=driving";
        String supplementMode = "driving";
        String coordTypeOutput = "gcj02";
        String sortType = "asc";
        int pageIndex = 0;
        int pageSize = 0;
        if (optionalParam != null && !optionalParam.isEmpty()) {
            if (StringUtils.isNotEmpty(optionalParam.get("isProcessed"))) {
                isProcessed = optionalParam.get("isProcessed");
            }
            if (StringUtils.isNotEmpty(optionalParam.get("processOption"))) {
                processOption = optionalParam.get("processOption");
            }
            if (StringUtils.isNotEmpty(optionalParam.get("supplementMode"))) {
                supplementMode = optionalParam.get("supplementMode");
            }
            if (StringUtils.isNotEmpty(optionalParam.get("coordTypeOutput"))) {
                coordTypeOutput = optionalParam.get("coordTypeOutput");
            }
            if (StringUtils.isNotEmpty(optionalParam.get("sortType"))) {
                sortType = optionalParam.get("sortType");
            }
        }
        GetTrackRequest getTrackRequest = new GetTrackRequest(mSequenceGenerator.incrementAndGet(), ak, serviceId,
                entityName, startTime, endTime, isProcessed, processOption, supplementMode, coordTypeOutput, sortType, pageIndex, pageSize);
        TrackResponse trackResponse = client.getTrack(getTrackRequest);
        return trackResponse;
    }
}
